package com.wiscamp.ninechapters.threeparties.alipay.resources;

import com.wiscamp.ninechapters.common.rest.ResourceHelper;
import com.wiscamp.ninechapters.common.rest.RestResponse;
import com.wiscamp.ninechapters.threeparties.alipay.entity.AlipayPreOrderParams;
import com.wiscamp.ninechapters.threeparties.alipay.entity.qrcode.QrServiceEntity;
import com.wiscamp.ninechapters.threeparties.alipay.service.AlipayService;
import cube.ddd.adapter.BaseController;
import cube.ddd.aspect.HandleResourceException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/ThreeParties")
@Tag(name = "支付-阿里支付")
public class AlipayController extends BaseController {
    private final AlipayService alipayService;

    public AlipayController(AlipayService alipayService) {
        this.alipayService = alipayService;
    }

    /**
     @POST
     @Path("alipay/refund")
     @Operation(summary = "退款")
     @HandleResourceException public RestResponse<RefundResponse> refund(@Context HttpServletRequest request, @Context HttpServletResponse response, RefundRequestParams refundRequestParams) {
     var result = alipayService.pcRefund(refundRequestParams);
     return ResourceHelper.getOkResponse(request, response, result);
     }


     @POST
     //@Path("alipay/refund/query")
     @Operation(summary = "退款查询")
     @HandleResourceException public RestResponse<RefundQueryResponse> refundQuery(@Context HttpServletRequest request, @Context HttpServletResponse response, RefundQueryParams refundQueryParams) {
     var result = alipayService.refundQuery(refundQueryParams);
     return ResourceHelper.getOkResponse(request, response, result);
     }

     @POST
     //@Path("alipay/qrcode")
     @Operation(summary = "获取支付二维码")
     @HandleResourceException public RestResponse<QrCodeResponse> qrcode(@Context HttpServletRequest request, @Context HttpServletResponse response, AlipayTradePrecreateModel model) {
     var result= alipayService.qrcodePay(model);
     return ResourceHelper.getOkResponse(request, response, result);
     }
     */

    /**
     * 支付回调
     *
     * @param request
     * @return
     */
    @PostMapping("alipay/notify")
    @Operation(summary = "支付异步回调")
    @HandleResourceException
    public String alipayNotify(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        QrServiceEntity qrServiceEntity = alipayService.alipayNotify(request);
        if (qrServiceEntity.getReceived()) {
            //return ResourceHelper.getOkResponse(request, response, Boolean.TRUE);
            return "success";
        } else {
            //return ResourceHelper.getOkResponse(request, response, Boolean.FALSE);
            return "fail";
        }
    }

    /*@GetMapping("alipay/return")
    @Operation(summary = "支付同步回调")
    @HandleResourceException
    public RestResponse<String> alipayReturn(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String result = alipayService.alipayReturn(request);
        return ResourceHelper.getOkResponse(request, response, result);
    }*/


    /**
     * 跳转到支付宝官网支付
     *
     * @param preOrderParams
     * @return
     */
    @PostMapping("alipay/preOrder/PC")
    @Operation(summary = "跳转到支付宝官网支付")
    @HandleResourceException
    public RestResponse<String> preOrderForPC(@Context HttpServletRequest request, @Context HttpServletResponse response, AlipayPreOrderParams preOrderParams) {
        var result = alipayService.pcPreOrder(preOrderParams);
        return ResourceHelper.getOkResponse(request, response, result);
    }

    /**
     @POST
     //@Path("alipay/pay")
     @Operation(summary = "支付成功回调页面, 返回")
     @HandleResourceException
     @RequestMapping(value = "success")
     public String successPage() {
     return "success";
     }
     */
}
